<?php
namespace Home\Controller;
use Think\Controller;

/***************************
****************************
	前台商家列表显示
****************************
****************************/
class ShopsController extends CommonController {
	 //保存微信openid  
    private $weixin_openid = ""; 
    //保存从微信得到的code   
	private $code="";
    //获得菜单中设置的state值  
    private $state="";
     //获得用户信息  
    private $userinfor="";
    //默认进行的函数 获取到用户的openid并且存到session里面
    public function _initialize(){
      
        if (isset($_GET['code']) && isset($_GET['state']) ){  
        $this->weixin_openid = GetOpenid($_GET['code']);  
        $this->code = $_GET['code'];  
        $this->state = $_GET['state']; 
	        if(session("weixin_openid") == "" || session("weixin_openid")=="code is invalid."){
	        	session("weixin_openid",$this->weixin_openid);
	        }
        }else{  
            return ;  
        }  
        //把openid存入session
        //$_SESSION['weixin_openid']=$this->weixin_openid;  
        // session("weixin_openid","o9Hr_syQN_1HPN9KLr7pqwwYwCvM");
        // $this->userinfor=$this->getinfobyopenid($this->weixin_openid); //获取用户信息
    }




	public function sdktest(){
		$this->display("Business/store");
	}

	//显示所有商家列表
	public function shopSelect(){
		$userinfo=$this->getinfobyopenid(I("session.weixin_openid"));
		// dump($userinfo);
		// dump($this->userinfor);
		// $shop=new \Home\Model\SyncModel("buss","kl_");
		$kind=I("get.kind");					//需要显示商家的类型
		if ($kind=="food") {
			$this->assign('kind',$kind);
			
		}
		else if ($kind=="movie") {
			$this->assign('kind',$kind);
		}
		// else{
		// 	$shoplist=$shop->shopQuery("2");
		// 	$this->assign('list',$shoplist);
		// }
		
		// dump($this->userinfor);
		
		$this->display("Business/store");
	}

	//根据前台ajax发送过来的搜索内容显示商家列表
	public function shopSearch(){
		$shop=new \Home\Model\SyncModel("buss","kl_");
		$type=I("post.type");
		$lat=I("post.lat");
		$lon=I("post.lon");
		$infor=I("post.infor");
		$kind=I("post.kind");			//商户类型
		$shoplist=$shop->shopQuery($type,$kind,$infor,$lat,$lon);
		// dump($shoplist);
		echo(json_encode($shoplist));
	}
	//显示地图
	public function showMap(){
		$lat=I("get.lat");
		$lon=I("get.lon");
		$this->assign('lat',$lat);
		$this->assign('lon',$lon);
		$this->display("Business/map");
	}
	//查询1Km内的商家
	public function shopsAround(){
		$shop=new \Home\Model\SyncModel("buss","kl_");
		$lat=I("post.lat");
		$lon=I("post.lon");
		$shoplist=$shop->shopQuery("4",null,$lat,$lon);
		echo(json_encode($shoplist));
	}

	//显示商家详情
	public function shopDetails(){
		// dump(I("session.weixin_openid"));
		// $userinfo=$this->getinfobyopenid(I("session.weixin_openid"));
		$shopid=I("get.id");
		$details=new \Home\Model\SyncModel("buss","kl_");
		$shopinfor=$details->details($shopid);   //获取商店信息
		$couponinfor=$details->coupon($shopid);  //获取优惠信息
		$commentinfor=$details->comment($shopid); //获取评论信息
		$collection=$details->ifCollection($shopid,I("session.weixin_openid")); //判断当前用户是否收藏商家
		// dump($collection);
		$openid=$details->getUser();
		// $userlist=array();
		// foreach ($openid as $key => $value) {
		// 	// $user_obj=$this->getinfobyopenid($value["openid"]);
		// 	// dump($this->getinfobyopenid($value["openid"]));
		// 	$userlist[$key]=(array)$this->getinfobyopenid($value["openid"]);
		// 	// dump($user_obj);
		// 	// foreach ($user_obj as $item => $val) {

		// 	// 	$userlist[$key][$item]=$val;
		// 	// }
		// }
		// dump($userlist);

		//用户签到信息
		foreach ($commentinfor as $key => $value) {
			$user=(array)$this->getinfobyopenid($value["user"]);
			$commentinfor[$key]["user"]=$user;
		}
		// dump($commentinfor);
		// $usertoken=$details->getToken($shopid);  //获取用户Token值
		// $userlist=array();						
		// foreach ($usertoken as $key => $value) {
		// 	$user=$details->getUser($value);
		// 	echo($value[0]);
		// 	array_push($userlist,$user);
		// }
		// var_dump($userlist);

		//判断当前用户是否能签到
		$signinfor=$details->signInfor("o9Hr_s2vRN4uw6-cM1O_79alFBSY"); //获取签到信息accesstoken
		if (!empty($signinfor)) {
			$time=strtotime($signinfor[0]['time']);         //数据库获取的时间转成时间戳
			$nowtime=time();
			$diff=abs($time-$nowtime)/3600;			//获取时间差(按小时)
			$this->assign('signinfor',$diff);		//时差
		}else{
			$this->assign('signinfor',$signinfor[0]['time']);	//从未签到
		}
		
		$this->assign('shopinfor',$shopinfor);    		//商户信息
		$this->assign('couponinfor',$couponinfor);		//优惠信息
		$this->assign('commentinfor',$commentinfor);	//评论信息
		$this->assign('score',$shopinfor[0]['score']);	//商户评分
		$this->assign('collection',$collection[0]['user']);		//是否已收藏
		$this->assign('openid',I("session.weixin_openid"));		//当前登陆用户openid
		// dump($collection);
		// $this->assign('signinfor',$diff);
		$this->display("Business/shop");
	}

	//显示优惠列表
	public function showPrefe(){
		
		$this->display("Business/prefe");
	}

	//获取优惠列表数组并拼接至商户数组中
	public function getPrefe(){
		$lat=I("post.lat");
		$lon=I("post.lon");
		$prefer=new \Home\Model\SyncModel("buss","kl_");
		$preinfor=$prefer->getShop($lat,$lon);   //获取具有优惠信息的商店信息
		foreach ($preinfor as $key => $value) {
			$predetails=$prefer->getPre(null,$value['bs_id']);
			$preinfor[$key]['data']=$predetails;
		}
		// dump($preinfor);
		echo(json_encode($preinfor));

		// $predetails=$prefer->getPre();   //获取优惠信息
		// echo(count($predetails));
		// var_dump($predetails);

		// $this->assign('preinfor',$preinfor);
		// $this->assign('predetails',$predetails);
	}

	//显示优惠详情
	public function showPD(){
		// dump(I("session.weixin_openid"));
		$openid=I("session.weixin_openid");
		$prefeid=I("get.id");
		$shopid=I("get.shopid");
		// $prefeid=1;
		$details=new \Home\Model\SyncModel("buss","kl_");
		$predetails=$details->getPre($prefeid);   //获取优惠信息
		$signnum=$details->getSignnum($openid,$shopid);  //获取签到次数
		$this->assign('predetails',$predetails);
		$this->assign('signnum',$signnum);
		$this->display("Business/pd");
	}

	//当前用户收藏商家
	public function shopCollect(){
		$openid=I("post.openid");
		$shopid=I("post.shopid");
		// dump($openid);
		// dump($shopid);
		$collection=new \Home\Model\SyncModel("buss","kl_");
		$collectinfor=$collection->collect($openid,$shopid);   //插入收藏信息
		if ($collectinfor) {
			echo(json_encode(array("results"=>'1')));
		}
		else{
			echo(json_encode(array("results"=>'0')));
		}
	}

	//显示商家类型为美食的商家列表
	public function showGoodList(){

	}

	//显示商家类型为电影的商家列表
	public function showMovieList(){

	}
}